﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using ByHand.Data;
using ByHand.Services.Code;

namespace ByHand.Services.Implements
{
    public static class StatisticsService
    {
        private static ByHandDB GetData()
        {
            return (new ByHandDB(ConfigurationManager.ConnectionStrings["ByHandDB"].ToString()));
        }
        public static int CountClickBuy(DateTime fromDate, DateTime toDate)
        {
            using (var db = GetData())
            {
                return (from ol in db.Logs
                        where (ol.ActionType == (int)ELogActionType.Button_Frame_Click ||
                             ol.ActionType == (int)ELogActionType.Button_Html_Click ||
                             ol.ActionType == (int)ELogActionType.Button_Widget_Click ||
                             ol.ActionType == (int)ELogActionType.ByHand_Click ||
                             ol.ActionType == (int)ELogActionType.Button_Widget_ViewMore_Click
                             ) && ol.DateTime <= toDate && ol.DateTime >= fromDate
                        select ol).Count();
            }
        }
        public static int CountClickByUser(int userID) {
            using (var db = GetData())
            {
                string id = userID.ToString();
                return (from ol in db.Logs
                        where (ol.ActionType == (int)ELogActionType.Button_Frame_Click ||
                             ol.ActionType == (int)ELogActionType.Button_Html_Click ||
                             ol.ActionType == (int)ELogActionType.Button_Widget_Click ||
                             ol.ActionType == (int)ELogActionType.ByHand_Click ||
                             ol.ActionType == (int)ELogActionType.Button_Widget_ViewMore_Click
                             ) && ol.RefID == id
                        select ol).Count();
            }
        }
        public static int CountOrderByUser(int userID)
        {
            using (var db = GetData())
            {
                string id = userID.ToString();
                return(from ol in db.Orders
                        where ol.Status == (int)EOrderStatus.Order_Done && ol.RefID == userID
                        select ol).Count();
            }
        }
        public static float TotalMoneyByUser(int userID)
        {
            using (var db = GetData())
            {
                var o = (from ol in db.Orders
                         where ol.Status == (int)EOrderStatus.Order_Done && ol.RefID == userID
                         select ol).ToList();
                float total = 0;
                foreach (var item in o)
                {
                    total += item.Price * item.Quantity;
                }
                return total;
            }
        }
        public static int CountOrderSuccess(DateTime fromDate, DateTime toDate)
        {
            using (var db = GetData())
            {
                return (from ol in db.Orders
                        where ol.Status == (int)EOrderStatus.Order_Done && ol.BoughtDate < toDate && ol.BoughtDate >= fromDate
                        select ol).Count();
            }
        }
        public static float TotalMoneyOrderSuccess(DateTime fromDate, DateTime toDate)
        {
            using (var db = GetData())
            {
                var o = (from ol in db.Orders
                         where ol.Status == (int)EOrderStatus.Order_Done && ol.BoughtDate < toDate && ol.BoughtDate >= fromDate
                         select ol).ToList();
                float total = 0;
                foreach (var item in o)
                {
                    total += item.Price * item.Quantity;
                }
                return total;
            }
        }
        
        public static Dictionary<int, int> GetUserAndOrder(DateTime fromDate, DateTime toDate, int top)
        {
            using (var db = GetData())
            {
                Dictionary<int, int> result = new Dictionary<int, int>();
                var gruop = (from ol in db.Orders
                             where (ol.Status == (int)EOrderStatus.Order_Done)
                             && ol.BoughtDate < toDate && ol.BoughtDate >= fromDate && ol.RefID != 0
                             group ol by ol.RefID into g
                             orderby g.Count() descending
                             select g).Take(top).ToList();
                foreach (var item in gruop)
                {
                    result.Add(Convert.ToInt32(item.Key), item.Count());
                }
                return result;
            }
        }
        public static Dictionary<string, int> DomainAndOrder(DateTime fromDate, DateTime toDate, int top)
        {
            using (var db = GetData())
            {
                Dictionary<string, int> result = new Dictionary<string, int>();
                var gruop = (from ol in db.Orders
                             where (ol.Status == (int)EOrderStatus.Order_Done)
                             && ol.BoughtDate < toDate && ol.BoughtDate >= fromDate && ol.RefDomain != null && ol.RefDomain!="" && ol.RefID!=0
                             group ol by ol.RefDomain into g
                             orderby g.Count() descending
                             select g).Take(top).ToList();
                foreach (var item in gruop)
                {
                    result.Add(item.Key, item.Count());
                }
                return result;
            }
        }
        public static int GetClickByUser(DateTime fromDate, DateTime toDate, int userID)
        {
            var uid = userID.ToString();
            using (var db = GetData())
            {
                return (from ol in db.Logs
                        where (ol.ActionType == (int)ELogActionType.Button_Frame_Click ||
                        ol.ActionType == (int)ELogActionType.Button_Html_Click ||
                        ol.ActionType == (int)ELogActionType.Button_Widget_Click ||
                         ol.ActionType == (int)ELogActionType.ByHand_Click ||
                        ol.ActionType == (int)ELogActionType.Button_Widget_ViewMore_Click)
                        && ol.DateTime < toDate && ol.DateTime >= fromDate && ol.RefID == uid
                        select ol).Count();
            }
        }
        public static int GetClickByDomain(DateTime fromDate, DateTime toDate, string domain)
        {
            using (var db = GetData())
            {
                return (from ol in db.Logs
                        where (ol.ActionType == (int)ELogActionType.Button_Frame_Click ||
                        ol.ActionType == (int)ELogActionType.Button_Html_Click ||
                        ol.ActionType == (int)ELogActionType.Button_Widget_Click ||
                        ol.ActionType == (int)ELogActionType.Button_Widget_ViewMore_Click||
                        ol.ActionType == (int)ELogActionType.ByHand_Click)
                        && ol.DateTime < toDate && ol.DateTime >= fromDate && ol.RefDomain == domain
                        select ol).Count();
            }
        }
        public static float MoneyByUser(DateTime fromDate, DateTime toDate, int userID)
        {
            using (var db = GetData())
            {
                var gruop = (from ol in db.Orders
                             where (ol.Status == (int)EOrderStatus.Order_Done)
                             && ol.BoughtDate < toDate && ol.BoughtDate >= fromDate && ol.RefID == userID
                             select ol).ToList();
                int result = 0;
                foreach (var item in gruop)
                {
                    result += item.Quantity * item.Price;
                }
                return result;
            }
        }
        public static float MoneyByDomain(DateTime fromDate, DateTime toDate, string domain)
        {
            using (var db = GetData())
            {
                var gruop = (from ol in db.Orders
                             where (ol.Status == (int)EOrderStatus.Order_Done)
                             && ol.BoughtDate < toDate && ol.BoughtDate >= fromDate && ol.RefDomain == domain
                             select ol).ToList();
                int result = 0;
                foreach (var item in gruop)
                {
                    result += item.Quantity * item.Price;
                }
                return result;
            }
        }
        public static List<string> GetDomainByUser(int userID) {
            using (var db = GetData())
            {
                string id = userID.ToString();
                return (from ol in db.Logs
                        where (ol.ActionType == (int)ELogActionType.Button_Frame_Click ||
                             ol.ActionType == (int)ELogActionType.Button_Html_Click ||
                             ol.ActionType == (int)ELogActionType.Button_Widget_Click ||
                             ol.ActionType == (int)ELogActionType.ByHand_Click ||
                             ol.ActionType == (int)ELogActionType.Button_Widget_ViewMore_Click
                             ) && ol.RefID == id
                        group ol by ol.RefDomain into g
                        select g.Key).ToList();
            }
        }
        public static int GetClickByDomain(string domain,int userID)
        {
            string uid = userID.ToString();
            using (var db = GetData())
            {
                return (from ol in db.Logs
                        where (ol.ActionType == (int)ELogActionType.Button_Frame_Click ||
                        ol.ActionType == (int)ELogActionType.Button_Html_Click ||
                        ol.ActionType == (int)ELogActionType.Button_Widget_Click ||
                        ol.ActionType == (int)ELogActionType.Button_Widget_ViewMore_Click ||
                        ol.ActionType == (int)ELogActionType.ByHand_Click)
                        && ol.RefDomain == domain && ol.RefID == uid
                        select ol).Count();
            }
        }
        public static int GetOrderByDomain(string domain,int userID)
        {
            using (var db = GetData())
            {
                return (from ol in db.Orders
                        where ol.Status == (int)EOrderStatus.Order_Done && ol.RefDomain == domain && ol.RefID == userID
                        select ol).Count();
            }
        }
        public static float GetMoneyByDomain(string domain, int userID)
        {
            using (var db = GetData())
            {
                var group = (from ol in db.Orders
                             where (ol.Status == (int)EOrderStatus.Order_Done)
                             && ol.RefDomain == domain && ol.RefID == userID
                             select ol).ToList();
                float result = 0;
                foreach (var item in group)
                {
                    result += item.Quantity * item.Price;
                }
                return result;
            }
        }
    }
}
